/*
 * Copyright (C) 2012 Fabien Barbero
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights 
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.flickr.activities;

import java.util.List;
import java.util.Vector;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import com.flickr.R;
import com.flickr.api.Paginated;
import com.flickr.api.photos.Photo;
import com.flickr.callbacks.PhotosLoaderCallback;
import com.flickr.widgets.PhotoFragment;


public abstract class AbstractGalleryActivity extends FragmentActivity implements PhotosLoaderCallback, OnItemClickListener
{
    
    private ViewPager pager;
//    private HorizontalListView photosList;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.gallery_activity);
        
        pager = (ViewPager) findViewById(R.id.galleryPhotosPager);
//        photosList = (HorizontalListView) findViewById(R.id.galleryPhotosList);
        
        pager.setPageMargin(10);
//        photosList.setOnItemClickListener(this);
    }
    
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        pager.setCurrentItem(position, true);
    }
    
    @Override
    public void onPhotosLoaded(Paginated<Photo> paginated)
    {
        List<Fragment> fragments = new Vector<Fragment>();
        for(Photo photo : paginated) {
            fragments.add(PhotoFragment.newInstance(photo));
        }
        pager.setAdapter(new GalleryPagerAdapter(getSupportFragmentManager(), fragments));
        
//        photosList.setAdapter(new PhotosAdapter(paginated, this));
    }
    
    protected static class GalleryPagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> mFragments;

        public GalleryPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            mFragments = fragments;
        }
        
        @Override
        public Fragment getItem(int position)
        {
            return mFragments.get(position);
        }

        @Override
        public int getCount()
        {
            return mFragments.size();
        }
        
    }
    
}